<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          // 查询关键字
          query: '',
          // 分类ID
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页长度
          pagesize: 10
        },
        goodsList: [],
        // 总数
        total: 0,
        // 节流阀
        isLoading: false
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // 调用请求获取商品分类数据的方法
      this.getGoodsList()
    },
    // 上拉监听
    onReachBottom() {
      // 判断当前数据是否已经加载完
      if (this.queryObj.pagesize * this.queryObj.pagesize >= this.total ) return uni.$showMsg('数据已加载完！')
      if (this.isLoading) return
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    // 上拉监听
    onPullDownRefresh() {
      // 重置数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.isLoading = false
      this.goodsList = []
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
    methods: {
      // 发送请求获取商品分类数据的方法
      async getGoodsList(cb) {
        //判断回调函数是否存在
        cb && cb()
        this.isLoading = true
        const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isLoading = false
        if (res.meta.status !== 200) return uni.$showMsg()
        // 获取第二页数据并合并
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      gotoDetail(goods) {
        uni.navigateTo({
          url: `/subpkg/goods_detail/goods_detail?goods_id=${goods.goods_id}`
        })
      }
      
    }
  }
</script>

<style lang="scss" scoped>

</style>
